package com.yicong.ants.ui.video;

import android.view.View;

import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.ui.activity.BaseStatefulActivity;
import com.cchao.simplelib.ui.adapter.DataBindQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.video.VideoHomeListBean;
import com.yicong.ants.databinding.ActivityVideoHomeDetailBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.ui.video.manager.OnViewPagerListener;
import com.yicong.ants.ui.video.manager.ViewPagerLayoutManager;
import com.yicong.ants.utils.JsonUtils;
import com.yicong.ants.utils.StatusBarUtil;
import com.yicong.ants.view.JzvdStdTikTok;

import java.util.List;

import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;

public class VideoHomeDetailActivity extends BaseStatefulActivity<ActivityVideoHomeDetailBinding> implements View.OnClickListener {
    private RecyclerView rvTiktok;
    private ViewPagerLayoutManager mViewPagerLayoutManager;
    private int mCurrentPosition = -1;
    DataBindQuickAdapter<VideoHomeListBean> mVideoAdapter;
    private int mPosition;

    @Override
    protected int getLayout() {
        return R.layout.activity_video_home_detail;
    }

    @Override
    protected void initEventAndData() {
        StatusBarUtil.setStatusImmerAndTextWhite(getWindow());
        mDataBind.setClick(this);
        mPosition = getIntent().getIntExtra(Constants.Extra.VIDEO_LIST_INDEX, 0);

        mViewPagerLayoutManager = new ViewPagerLayoutManager(mContext, OrientationHelper.VERTICAL);
        mViewPagerLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                autoPlayVideo(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurrentPosition == position) {
                    Jzvd.releaseAllVideos();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) {
                    return;
                }
                autoPlayVideo(position);
                mCurrentPosition = position;
            }
        });
        mViewPagerLayoutManager.scrollToPosition(mPosition);

        initAdapter();
    }

    private void initAdapter() {
        rvTiktok = mDataBind.rvTiktok;
        rvTiktok.setLayoutManager(mViewPagerLayoutManager);
        rvTiktok.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource != null &&
                    jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });

        mVideoAdapter = new DataBindQuickAdapter<VideoHomeListBean>(R.layout.item_video_home_detail) {
            @Override
            protected void convert(DataBindViewHolder helper, VideoHomeListBean item) {
                helper.getBinding().setVariable(BR.bean, item);
                helper.getBinding().setVariable(BR.click, (View.OnClickListener) view -> {
                    switch (view.getId()) {
                        case R.id.gift:
                            getGiftList();
                            break;
                    }
                });

                JzvdStdTikTok view = helper.getView(R.id.videoplayer);
                JZDataSource jzDataSource = new JZDataSource(AppHelper.getProxy().getProxyUrl(item.getUrl()));
                view.setUp(jzDataSource, Jzvd.SCREEN_NORMAL);
                jzDataSource.looping = true;
            }
        };
        List<VideoHomeListBean> videoHomeListBeanList = JsonUtils.fromJson(getIntent().getStringExtra(Constants.Extra.VIDEO_LIST), new TypeToken<List<VideoHomeListBean>>() {
        }.getType());
        mVideoAdapter.setNewData(videoHomeListBeanList);
        rvTiktok.setAdapter(mVideoAdapter);
    }

    private void getGiftList() {
    }


    @Override
    protected void onLoadData() {
    }

    @Override
    public void onClick(View view) {

    }

    private void autoPlayVideo(int postion) {
        if (rvTiktok == null || rvTiktok.getChildAt(0) == null) {
            return;
        }
        JzvdStdTikTok player = rvTiktok.getChildAt(0).findViewById(R.id.videoplayer);

        if (player != null) {
            player.startVideoAfterPreloading();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }
}
